<template>
  <div class="jsbos-common-layout">
    <div class="jsbos-common-layout-center">
      <div class="jsbos_search">
        <el-form @submit.native.prevent label-width="120px">
          <el-form-item label="名称">
            <el-input
              v-model="query.name"
              placeholder="请输入"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="目录">
            <TreeSelect
              v-model="query.categoryId"
              placeholder="请选择
                                        "
              :options="categoryIdOptions"
              :props="categoryIdProps"
              clearable
            />
          </el-form-item>
          <el-form-item label="开始时间">
            <el-date-picker
              v-model="query.startDatetime"
              type="datetimerange"
              value-format="timestamp"
              format="yyyy-MM-dd HH:mm:ss"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="培训形式">
            <el-select v-model="query.type" placeholder="请选择" clearable>
              <el-option
                v-for="(item, index) in typeOptions"
                :key="index"
                :label="item.fullName"
                :value="item.id"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="培训层级">
            <el-select v-model="query.grade" placeholder="请选择" clearable>
              <el-option
                v-for="(item, index) in gradeOptions"
                :key="index"
                :label="item.fullName"
                :value="item.id"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="状态">
            <el-select
              v-model="query.enabledMark"
              placeholder="请选择"
              clearable
            >
              <el-option
                v-for="(item, index) in enabledMarkOptions"
                :key="index"
                :label="item.fullName"
                :value="item.id"
                :disabled="item.disabled"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="创建用户">
            <userSelect v-model="query.creatorUserId" placeholder="请选择" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="search()"
              >查询</el-button
            >
            <el-button icon="el-icon-refresh-right" @click="reset()"
              >重置</el-button
            >
            <el-button
              type="text"
              icon="el-icon-arrow-down"
              class="open_search"
              @click="jsbos.openAndClose('open')"
            >
              展开
            </el-button>
            <el-button
              type="text"
              icon="el-icon-arrow-up"
              class="close_search"
              @click="jsbos.openAndClose('close')"
            >
              收起
            </el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="jsbos-common-layout-main jsbos-flex-main">
        <div class="jsbos-common-head">
          <div>
            <el-button
              type="primary"
              icon="el-icon-plus"
              @click="addOrUpdateHandle()"
              >新增
            </el-button>
            <el-button type="text" icon="el-icon-download" @click="exportData()"
              >导出
            </el-button>
          </div>
          <div class="jsbos-common-head-right">
            <el-tooltip effect="dark" content="刷新" placement="top">
              <el-link
                icon="icon-ym icon-ym-Refresh JSBOS-common-head-icon"
                :underline="false"
                @click="reset()"
              />
            </el-tooltip>
            <screen-full isContainer />
            <el-tooltip effect="dark" content="列设置" placement="top">
              <el-link
                icon="icon-ym icon-ym-options jsbos-common-head-icon"
                :underline="false"
                @click="() => this.$refs.jsbosTableRef.showDrawer()"
              />
            </el-tooltip>
          </div>
        </div>
        <jsbos-table
          v-loading="listLoading"
          :data="list"
          ref="jsbosTableRef"
          @sort-change="sortChange"
        >
          <el-table-column prop="name" label="名称" width="0" align="left" />
          <el-table-column
            prop="categoryId"
            label="目录"
            width="0"
            align="left"
          />
          <el-table-column
            prop="endDatetime"
            label="截至时间"
            width="0"
            align="left"
          />
          <el-table-column
            prop="startDatetime"
            label="开始时间"
            width="0"
            align="left"
          />
          <el-table-column
            prop="plan"
            label="是否关联培训计划"
            width="0"
            align="left"
          />

          <el-table-column label="培训形式" width="0" prop="type" algin="left">
            <template slot-scope="scope">
              {{ scope.row.type | dynamicText(typeOptions) }}
            </template>
          </el-table-column>

          <el-table-column label="培训层级" width="0" prop="grade" algin="left">
            <template slot-scope="scope">
              {{ scope.row.grade | dynamicText(gradeOptions) }}
            </template>
          </el-table-column>

          <el-table-column
            label="状态"
            width="0"
            prop="enabledMark"
            algin="left"
          >
            <template slot-scope="scope">
              <div>
                <span class="state-style-blue"></span>
                {{ scope.row.enabledMark | dynamicText(enabledMarkOptions) }}
              </div>
            </template>
          </el-table-column>
          <el-table-column
            prop="creatorUserId"
            label="创建用户"
            width="0"
            align="left"
          />
          <el-table-column
            prop="creatorTime"
            label="创建时间"
            width="0"
            align="left"
          />
          <el-table-column label="操作" fixed="right" width="150">
            <template slot-scope="scope">
              <el-button type="text" @click="addOrUpdateHandle(scope.row.id)"
                >编辑
              </el-button>
              <el-button
                type="text"
                class="jsbos-table-delBtn"
                @click="handleDel(scope.row.id)"
                >删除
              </el-button>
              <el-button type="text" @click="goDetail(scope.row.id)"
                >详情
              </el-button>
            </template>
          </el-table-column>
        </jsbos-table>
        <pagination
          :total="total"
          :page.sync="listQuery.currentPage"
          :limit.sync="listQuery.pageSize"
          @pagination="initData"
        />
      </div>
    </div>
    <JSBOS-Form v-if="formVisible" ref="JSBOSForm" @refresh="refresh" />
    <ExportBox v-if="exportBoxVisible" ref="ExportBox" @download="download" />
    <Detail
      v-if="detailVisible"
      ref="Detail"
      @refresh="detailVisible = false"
    />
  </div>
</template>

<script>
import request from "@/utils/request";
import JSBOSForm from "./Form.vue";
import ExportBox from "./ExportBox.vue";
import { getDataInterfaceRes } from "@/api/systemData/dataInterface";
import Detail from "./Detail.vue";

export default {
  name: "TemsProject",
  components: { JSBOSForm, ExportBox, Detail },
  data() {
    return {
      showAll: false,
      detailVisible: false,
      query: {
        name: undefined,
        categoryId: undefined,
        startDatetime: undefined,
        type: undefined,
        grade: undefined,
        enabledMark: undefined,
        creatorUserId: undefined,
      },
      treeProps: {
        children: "children",
        label: "fullName",
        value: "id",
      },
      list: [],
      listLoading: true,
      total: 0,
      listQuery: {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      },
      formVisible: false,
      exportBoxVisible: false,
      columnList: [
        { prop: "name", label: "名称" },
        { prop: "categoryId", label: "目录" },
        { prop: "endDatetime", label: "截至时间" },
        { prop: "startDatetime", label: "开始时间" },
        { prop: "plan", label: "是否关联培训计划" },
        { prop: "type", label: "培训形式" },
        { prop: "grade", label: "培训层级" },
        { prop: "enabledMark", label: "状态" },
        { prop: "creatorUserId", label: "创建用户" },
        { prop: "creatorTime", label: "创建时间" },
      ],
      categoryIdOptions: [],
      categoryIdProps: {
        label: "fullName",
        value: "enCode",
        children: "children",
      },
      typeOptions: [
        { fullName: "内部培训", id: "1" },
        { fullName: "外部培训", id: "2" },
        { fullName: "外请培训", id: "3" },
      ],
      typeProps: {
        label: "fullName",
        value: "id",
      },
      gradeOptions: [
        { fullName: "公司级", id: "1" },
        { fullName: "部门级", id: "2" },
      ],
      gradeProps: {
        label: "fullName",
        value: "id",
      },
      scopeTypeOptions: [
        { fullName: "全公司", id: "1" },
        { fullName: "仅创建人", id: "2" },
        { fullName: "指定人员", id: "3" },
      ],
      scopeTypeProps: {
        label: "fullName",
        value: "id",
      },
      signAuditOptions: [
        { fullName: "需要", id: "1" },
        { fullName: "不需要", id: "0" },
      ],
      signAuditProps: {
        label: "fullName",
        value: "id",
      },
      studentShowOptions: [
        { fullName: "显示", id: "1" },
        { fullName: "不显示", id: "2" },
      ],
      studentShowProps: {
        label: "fullName",
        value: "id",
      },
      enabledMarkOptions: [
        { fullName: "未发布", id: "0" },
        { fullName: "已发布", id: "1" },
      ],
      enabledMarkProps: {
        label: "fullName",
        value: "id",
      },
    };
  },
  computed: {
    menuId() {
      return this.$route.meta.modelId || "";
    },
  },
  created() {
    this.initData();
    this.getCategoryIdOptions();
  },
  methods: {
    getCategoryIdOptions() {
      this.$store
        .dispatch("base/getDictionaryData", {
          encode: "KnowledgeCategory",
        })
        .then((res) => {
          this.categoryIdOptions = res;
        });
    },
    goDetail(id) {
      this.detailVisible = true;
      this.$nextTick(() => {
        this.$refs.Detail.init(id);
      });
    },
    sortChange({ column, prop, order }) {
      this.listQuery.sort = order == "ascending" ? "asc" : "desc";
      this.listQuery.sidx = !order ? "" : prop;
      this.initData();
    },
    initData() {
      this.listLoading = true;
      let _query = {
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/course/TemsProject/getList`,
        method: "post",
        data: _query,
      }).then((res) => {
        var _list = [];
        for (let i = 0; i < res.data.list.length; i++) {
          let _data = res.data.list[i];
          _list.push(_data);
        }
        this.list = _list;
        this.total = res.data.pagination.total;

        this.listLoading = false;
      });
    },
    handleDel(id) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        type: "warning",
      })
        .then(() => {
          request({
            url: `/api/course/TemsProject/${id}`,
            method: "DELETE",
          }).then((res) => {
            this.$message({
              type: "success",
              message: res.msg,
              onClose: () => {
                this.initData();
              },
            });
          });
        })
        .catch(() => {});
    },
    addOrUpdateHandle(id, isDetail) {
      this.formVisible = true;
      this.$nextTick(() => {
        this.$refs.JSBOSForm.init(id, isDetail);
      });
    },
    exportData() {
      this.exportBoxVisible = true;
      this.$nextTick(() => {
        this.$refs.ExportBox.init(this.columnList);
      });
    },
    download(data) {
      let query = {
        ...data,
        ...this.listQuery,
        ...this.query,
        menuId: this.menuId,
      };
      request({
        url: `/api/course/TemsProject/Actions/Export`,
        method: "GET",
        data: query,
      }).then((res) => {
        if (!res.data.url) return;
        this.jsbos.downloadFile(res.data.url);
        this.$refs.ExportBox.visible = false;
        this.exportBoxVisible = false;
      });
    },
    search() {
      this.listQuery = {
        currentPage: 1,
        pageSize: 20,
        sort: "desc",
        sidx: "",
      };
      this.initData();
    },
    refresh(isrRefresh) {
      this.formVisible = false;
      if (isrRefresh) this.reset();
    },
    reset() {
      for (let key in this.query) {
        this.query[key] = undefined;
      }
      this.search();
    },
  },
};
</script>
